package com.boss.salary.dao.impl;

import com.boss.framework.dao.BaseDaoImpl;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.constant.TableName;
import com.boss.salary.dao.IdepartmentInfoDao;
import com.boss.salary.entity.DepartmentInfo;
import com.boss.sys.utils.SysUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class DepartmentInfoDaoImpl extends BaseDaoImpl implements IdepartmentInfoDao {

    @Override
    public void insertDepartmentInfo(DepartmentInfo departmentInfo) {
        StringBuilder sql=new StringBuilder();
        sql.append("insert into ").append(TableName.DEPARTMENT_INFO);
        sql.append("(id, create_time, update_time, agency_id, dept_name,")
           .append("parent_id, level_no, is_leaf, dept_code, dept_abbreviation,")
           .append("dept_type, dept_state, is_deleted, mof_div_code)")
           .append(" value(?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
        Object[] params = new Object[]{
                departmentInfo.getId(),departmentInfo.getCreateTime(),departmentInfo.getUpdateTime(),departmentInfo.getAgencyId(),departmentInfo.getDeptName(),
                departmentInfo.getParentId(),departmentInfo.getLevelNo(),departmentInfo.getIsLeaf(),departmentInfo.getDeptCode(),departmentInfo.getDeptAbbreviation(),
        departmentInfo.getDeptType(),departmentInfo.getDeptState(),departmentInfo.getIsDeleted(),departmentInfo.getMofDivCode()
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public void deleteDepartmentInfo(String id) {
        StringBuilder sql = new StringBuilder();
        sql.append("update ").append(TableName.DEPARTMENT_INFO);
        sql.append(" set is_deleted='1' ")
                .append("where id=?");
        Object[] params = new Object[]{
                id
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public void updateDepartmentInfo(DepartmentInfo departmentInfo) {
        StringBuilder sql = new StringBuilder();
        sql.append("update ").append(TableName.DEPARTMENT_INFO);
        sql.append(" set create_time=?, update_time=?, agency_id=?, dept_name=?, parent_id=?,")
           .append("level_no=?, is_leaf=?, dept_code=?, dept_abbreviation=?, dept_type=?,")
           .append("dept_state=?, is_deleted=?  where id=?");
        Object[] params = new Object[]{
                departmentInfo.getCreateTime(),departmentInfo.getUpdateTime(),departmentInfo.getAgencyId(),departmentInfo.getDeptName(),
                departmentInfo.getParentId(),departmentInfo.getLevelNo(),departmentInfo.getIsLeaf(),departmentInfo.getDeptCode(),departmentInfo.getDeptAbbreviation(),
                departmentInfo.getDeptType(),departmentInfo.getDeptState(),departmentInfo.getIsDeleted(), departmentInfo.getId(),
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public int queryDepartmentInfo(Map<String, Object> queryParam,List<String> childrenId) {
        StringBuilder sql = new StringBuilder();
        List<Object> Params = new ArrayList<>();
        sql.append("select  count(*) from ").append(TableName.DEPARTMENT_INFO);

            sql.append(" where  agency_id=? and  mof_div_code=? ");

            Params.add(queryParam.get("agencyId"));
            Params.add(queryParam.get("mofDivCode"));
            sql.append(" and ").append(SysUtils.inSql("id",childrenId));

        return this.getJdbcTemplate().queryForObject(sql.toString(),Integer.class,Params.toArray());
    }

    @Override
    public List<DepartmentInfo> queryDepartmentInfoPage(PaginationDTO paginationDTO,Map<String, Object> queryParam,List<String> childrenId) {
        StringBuilder sql = new StringBuilder();
        List<Object> Params = new ArrayList<>();
        sql.append("select id, create_time, update_time, agency_id, dept_name,")
           .append("parent_id, level_no, is_leaf, dept_code, dept_abbreviation,")
           .append("dept_type, dept_state, is_deleted from ")
           .append(TableName.DEPARTMENT_INFO);

            sql.append(" where agency_id=? and mof_div_code=?");
        Params.add(queryParam.get("agencyId"));
        Params.add(queryParam.get("mofDivCode"));
              sql.append(" and  ").append(SysUtils.inSql("id",childrenId));

        sql.append(" order by dept_code, create_time");
        String querySql = this.boxUpSql(sql.toString());
        //请求参数初始化
        Object[] params = this.boxUpParam(Params.toArray(), paginationDTO);
        return this.getJdbcTemplate().query(querySql, new BeanPropertyRowMapper(DepartmentInfo.class), params);
    }

    @Override
    public List<Map<String, String>> getBaseDataSelect(String parameters) {
        StringBuilder sql = new StringBuilder();
        List<Map<String, String>> resultList = new ArrayList<>();
        sql.append("select id, create_time, update_time, agency_id, dept_name,")
                .append("parent_id, level_no, is_leaf, dept_code, dept_abbreviation,")
                .append("dept_type, dept_state, is_deleted from ")
                .append(TableName.DEPARTMENT_INFO);
        sql.append(" where agency_id=?");
        sql.append(" order by create_time");


        //请求参数初始化
        Object[] params = new Object[]{parameters};
        List<Map<String, Object>> relist=this.getJdbcTemplate().queryForList(sql.toString(),params);

        for(int i=0;i<relist.size();i++){
            Map<String,String> rm= new HashMap<>();
            rm.put("code",relist.get(i).get("id").toString());
            rm.put( "name",relist.get(i).get("dept_name").toString());
            resultList.add(rm);
        }
        return resultList;
    }

    @Override
    public List<DepartmentInfo> getAllDepartment(String agencyid,String mofDivCode) {
        StringBuilder sql = new StringBuilder();
        List<Object> params = new ArrayList<>();
        sql.append("select id, create_time, update_time, agency_id, dept_name,")
                .append("parent_id, level_no, is_leaf, dept_code, dept_abbreviation,")
                .append("dept_type, dept_state, is_deleted from ")
                .append(TableName.DEPARTMENT_INFO)
                .append(" where is_deleted=? ");
        params.add(SystemDictConstant.IS_FALSE);

            sql.append(" and agency_id=? and mof_div_code=?");
            params.add(agencyid);
            params.add(mofDivCode);
        sql.append(" order by dept_code");

        return this.getjdbcTemplate().query(sql.toString(),new BeanPropertyRowMapper(DepartmentInfo.class),params.toArray());
    }

    @Override
    public void updatelIsLeaf(String id,Integer is_leaf) {
        StringBuilder sql = new StringBuilder();
        sql.append("update ").append(TableName.DEPARTMENT_INFO);
        sql.append(" set is_leaf=? where id=?");

        Object[] params = new Object[]{
                is_leaf,  id
        };
        this.getJdbcTemplate().update(sql.toString(), params);
    }

    @Override
    public DepartmentInfo getDepartmentInfoById(String id) {
        StringBuilder sql = new StringBuilder();
        List<Object> Params = new ArrayList<>();
        sql.append("select id, create_time, update_time, agency_id, dept_name,")
                .append("parent_id, level_no, is_leaf, dept_code, dept_abbreviation,")
                .append("dept_type, dept_state, is_deleted from ")
                .append(TableName.DEPARTMENT_INFO)
                .append(" where is_deleted=0 and id=?");
        Params.add(id);
        return this.getjdbcTemplate().queryForObject(sql.toString(),new BeanPropertyRowMapper<>(DepartmentInfo.class),Params.toArray());
    }

    @Override
    public int getNumByParentId(String parentid,String agencyId) {
        StringBuilder sql = new StringBuilder();
        List<Object> params = new ArrayList<>();
        sql.append("select count(*) from ").append(TableName.DEPARTMENT_INFO)
                .append(" where agency_id=? ");
        params.add(agencyId);
        if(!StringUtils.isEmpty(parentid)){
            sql.append(" and parent_id=? ");
            params.add(parentid);
        }else {
            sql.append(" and level_no=? ");
            params.add(1);
        }
        return this.getJdbcTemplate().queryForObject(sql.toString(), Integer.class,params.toArray());
    }

    @Override
    public int getNumByLevel1(String agencyid) {
        StringBuilder sql = new StringBuilder();
        List<Object> Params = new ArrayList<>();
        sql.append("select count(*) from  ").append(TableName.DEPARTMENT_INFO);

        sql.append(" where  level_no=?");
        Params.add(SystemDictConstant.ENABLE);

        if(!StringUtils.isEmpty(agencyid)){
            sql.append(" and agency_id=?");
            Params.add(agencyid);
        }

        return this.getJdbcTemplate().queryForObject(sql.toString(), Integer.class,Params.toArray());
    }

    @Override
    public int getNumByName(String name,String agencyid,String id) {
        StringBuilder sql = new StringBuilder();
        List<Object> Params = new ArrayList<>();
        sql.append("select count(*) from ").append(TableName.DEPARTMENT_INFO)
           .append(" where dept_name=?");
        Params.add(name);
        sql.append(" and  agency_id=?");
        Params.add(agencyid);
        sql.append(" and id<>?");
        Params.add(id);
        return this.getJdbcTemplate().queryForObject(sql.toString(), Integer.class,Params.toArray());
    }

    @Override
    public String getNameById(String agencyId, String id) {
        StringBuilder sql = new StringBuilder();
        sql.append("select dept_name from ").append(TableName.DEPARTMENT_INFO)
           .append(" where agency_id=? and id=? and is_deleted=?");
        Object[] params = new Object[]{
                agencyId,id,SystemDictConstant.IS_FALSE
        };
        return this.getJdbcTemplate().queryForObject(sql.toString(), String.class, params);
    }

    @Override
    public List<DepartmentInfo> getAgencyDept(String agencyId) {
        StringBuilder sql = new StringBuilder();
        sql.append("select dept_name,id from ").append(TableName.DEPARTMENT_INFO)
                .append(" where agency_id=?  and is_deleted=?");
        Object[] params = new Object[]{
                agencyId,SystemDictConstant.IS_FALSE
        };

        return this.getjdbcTemplate().query(sql.toString(),new BeanPropertyRowMapper(DepartmentInfo.class),params);
    }

    @Override
    public List<String> selectDeptCodeByPID(String pid,String agencyId) {
        StringBuilder sql = new StringBuilder();
        List<Object> params = new ArrayList<>();
        sql.append("select  dept_code from ")
                .append(TableName.DEPARTMENT_INFO);
        sql.append(" where  is_deleted=? and agency_id=? ");
        params.add(SystemDictConstant.NOT_DELETED);
        params.add(agencyId);
        if(!StringUtils.isEmpty(pid)&&!"0".equals(pid)){
            sql.append(" and parent_id=? ");
            params.add(pid);
        }

        return super.getjdbcTemplate().queryForList(sql.toString(), String.class, params.toArray());
    }

}
